﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TeenyGrab.Objects;
using System.Drawing;
using TeenyGrab.Helpers;
using System.Windows.Forms;

namespace TeenyGrab.Windows
{
    /// <summary>
    /// Interaction logic for AreaSelectorWindow.xaml
    /// </summary>
    public partial class AreaSelectorWindow : Window
    {
        private System.Drawing.Image Image { get; set; }
        public bool TopGrabbed { get; set; }
        public bool LeftGrabbed { get; set; }
        public bool BottomGrabbed { get; set; }
        public bool RightGrabbed { get; set; }
        public Area Area { get; set; }
        private bool Done { get; set; }
        private bool ShowMessage { get; set; }

        public AreaSelectorWindow(System.Drawing.Image image)
        {
            InitializeComponent();

            Image = image;

            image1.Source = new Bitmap(Image).ToBitmapImage();

            ShowMessage = true;
            Done = false;
            var pScreen = Screen.PrimaryScreen;
            Area = new Area(pScreen.Bounds.Width / 4,
                            pScreen.Bounds.Height / 4,
                           (pScreen.Bounds.Width / 4) * 3,
                           (pScreen.Bounds.Height / 4) * 3);
            UpdateRectangle();

            this.Width = Image.Width;
            this.Height = Image.Height;

            var screenWidth = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            var screenHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;

            TopGrabbed = LeftGrabbed = BottomGrabbed = RightGrabbed = false;

            this.Left = 0;//(screenWidth / 2) - (Width / 2);
            this.Top = 0;// (screenHeight / 2) - (Height / 2);
        }

        private void image1_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                //ShowMessage = false;
                var mousePos = e.GetPosition(image1);
                if (LeftGrabbed)
                {
                    Area.X = (int)mousePos.X;
                    if (Area.X >= Area.X2)
                    {
                        Area.X = Area.X2-1;
                    }
                }
                else if (RightGrabbed)
                {
                    Area.X2 = (int)mousePos.X;
                    if (Area.X2 <= Area.X)
                    {
                        Area.X2 = Area.X+1;
                    }
                } 

                if (TopGrabbed)
                {
                    Area.Y = (int)mousePos.Y;
                    if (Area.Y >= Area.Y2)
                    {
                        Area.Y = Area.Y2-1;
                    }
                }
                else if (BottomGrabbed)
                {
                    Area.Y2 = (int)mousePos.Y;
                    if (Area.Y2 <= Area.Y)
                    {
                        Area.Y2 = Area.Y+1;
                    }
                }
                UpdateRectangle();
            }
        }

        private void image1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            var mousePos = e.GetPosition(image1);

            LeftGrabbed = (Math.Abs(mousePos.X - Area.X) < Math.Abs(mousePos.X - Area.X2));
            RightGrabbed = !LeftGrabbed;

            TopGrabbed = (Math.Abs(mousePos.Y - Area.Y) < Math.Abs(mousePos.Y - Area.Y2));
            BottomGrabbed = !TopGrabbed;
        }

        private void image1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            TopGrabbed = LeftGrabbed = BottomGrabbed = RightGrabbed = false;
            UpdateRectangle();
        }

        private void UpdateRectangle()
        {
            if (ShowMessage)
            {
                message.Margin = new Thickness(Area.X, Area.Y - 35, 0, 0);
                message.Width = Area.X2 - Area.X;
            }
            else
            {
                message.Text = "";
            }
            rectangle.Margin = new Thickness(Area.X, Area.Y, 0, 0);
            rectangle.Width = Area.X2 - Area.X;
            rectangle.Height = Area.Y2 - Area.Y;

            topLeft.Margin = new Thickness(Area.X-topLeft.Width, Area.Y-topLeft.Height, 0, 0);
            topRight.Margin = new Thickness(Area.X2, Area.Y-topRight.Height, 0, 0);
            bottomLeft.Margin = new Thickness(Area.X-bottomLeft.Width, Area.Y2, 0, 0);
            bottomRight.Margin = new Thickness(Area.X2, Area.Y2, 0, 0);
        }

        private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (Done) return;
                Done = true;

                this.DialogResult = true;
                this.Close();
            }
        }
    }
}
